package com.jojo.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jojo.store.entity.Address;
import com.jojo.store.mapper.AddressMapper;
import com.jojo.store.service.IAddressService;
import com.jojo.store.service.IDistrictService;
import com.jojo.store.service.ex.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AddressServiceImpl implements IAddressService {
    @Autowired
    private AddressMapper addressMapper;
    @Autowired
    private IDistrictService districtService;


    @Value("${user.address.max-count}")
  private Integer maxCount;
    @Override
    public void addAddress(Address address, Integer uid, String username) {
        Integer counts = addressMapper.countByUid(uid);
        System.out.println("这里是业务增加类 counts="+counts);
        if (counts==null){
            counts=0;
        }
        if (counts>=maxCount){
            throw new AddressCountLimitException("用户地址已达到限制要求无法添加");
        }
        String provinceName = districtService.getByCode(address.getProvinceCode());
        String cityname = districtService.getByCode(address.getCityCode());
        String areaname = districtService.getByCode(address.getAreaCode());
        address.setProvinceName(provinceName);
        address.setCityName(cityname);
        address.setAreaName(areaname);
        address.setUid(uid);
        Integer isDefault=counts==0? 1 : 0;
        address.setIsDefault(isDefault);
        address.setCreatedTime(new Date());
        address.setCreatedUser(username);
        address.setModifiedTime(new Date());
        address.setModifiedUser(username);
        int insert = addressMapper.insert(address);
        if (insert!=1){
            throw new InsertException("插入发生未知异常");
        }

    }

    @Override
    public List<Address> findByuidAddress(Integer uid) {

        List<Address> resultAddress = addressMapper.findByUid(uid);
        for (Address address : resultAddress) {
            address.setAreaName(null);
            address.setProvinceName(null);
            address.setCityName(null);
            address.setZip(null);
            address.setTel(null);
            address.setCreatedUser(null);
            address.setModifiedUser(null);
            address.setModifiedTime(null);
            address.setCreatedTime(null);
        }
        return resultAddress;
    }

    @Override
    public void changeDefault(Integer uid, Integer aid,String username) {
        Address result = addressMapper.selectOne(new QueryWrapper<Address>().eq("aid", aid));
        if (result==null){
            throw new AddressNotFontException("地址数据不存在");
        }
        if (!result.getUid().equals(uid)){
            throw new AccessDeniedException("改用户的地址不属于用户的异常");
        }
        Integer result0 = addressMapper.upDefault0(uid);
        if (result0<=0){
            throw new updateException("更新时发生未知异常第一个");
        }
        int update = addressMapper.update(null, new UpdateWrapper<Address>().eq("aid", aid).set("is_default", 1).set("modified_user", username).set("modified_time", new Date()));
        if(update!=1 ){
            throw new updateException("更新时发生未知异常");
        }

    }

    @Override
    public IPage<Address> getAllBypage(Page<Address> page, Integer uid) {

        return addressMapper.findAll(page,uid);
    }

    @Override
    public void deleteAndDef(Integer uid, Integer aid,String username) {
        Address aidresult = addressMapper.selectOne(new QueryWrapper<Address>().eq("aid", aid));
        if (aidresult==null){
            throw new UnameNotFoundException("地址数据不存在");
        }
        Integer integer = addressMapper.deleteByaid(aid);
        if (integer!=1){
            throw new DeleteException("删除出现异常");
        }
        Integer resucount = addressMapper.countByUid(uid);
        if (resucount==null){
            resucount=0;
            return;
        }
        Address lastmod = addressMapper.findLastmod(11);
        if (aidresult.getIsDefault()==1){
            this.changeDefault(uid,lastmod.getAid(),username);
        }

    }
}
